package com.javaweb.dao.impl;

import com.javaweb.common.StringUtils;
import com.javaweb.dao.PermDao;
import com.javaweb.dao.UserDao;
import com.javaweb.model.Permission;
import com.javaweb.model.User;
import com.sun.source.tree.TryTree;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @desc: PermDao implementation
 * @date: 2024/9/24 22:43
 */
@Repository
@Slf4j
public class PermDaoImpl implements PermDao {

    @Autowired
    private Connection conn;

    @Override
    public Integer insert(Permission perm) {
        String sql = "INSERT INTO tb_permission (name, url) VALUES (?, ?)";
        try (PreparedStatement ps = this.conn.prepareStatement(sql)) {
            ps.setString(1, perm.getName());
            ps.setString(2, perm.getUrl());
            return ps.executeUpdate();
        } catch (SQLException e) {
            log.error("execute insert permission error: " + e.getMessage());
        }
        return 0;
    }

    @Override
    public Integer insertRolePerm(int roleId, int permId) {
        String sql = "INSERT INTO tb_role_perm (role_id, perm_id) VALUES (?, ?)";
        try (PreparedStatement ps = this.conn.prepareStatement(sql)) {
            ps.setInt(1, roleId);
            ps.setInt(2, permId);
            return ps.executeUpdate();
        } catch (SQLException e) {
            log.error("execute insertRolePerm error: " + e.getMessage());
        }
        return 0;
    }

    @Override
    public Permission selectOne(String name, String url) {
        String sql = "SELECT * FROM tb_permission WHERE name=? AND url=?";
        try (PreparedStatement ps = this.conn.prepareStatement(sql)) {
            ps.setString(1, name);
            ps.setString(2, url);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {

                Permission permission = new Permission();
                      permission.setPerId(rs.getInt("per_id"));
                        permission.setName(rs.getString("name"));
                        permission.setUrl(rs.getString("url"));
                return permission;
            }
        } catch (SQLException e) {
            log.error("execute selectOne error: " + e.getMessage());
        }
        return null;
    }

    @Override
    public Permission selectOneById(int permId) {
        String sql = "SELECT * FROM tb_permission WHERE per_id=?";
        try (PreparedStatement ps = this.conn.prepareStatement(sql)) {
            ps.setInt(1, permId);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                Permission permission = new Permission();
                permission.setPerId(rs.getInt("per_id"));
                permission.setName(rs.getString("name"));
                permission.setUrl(rs.getString("url"));
                return permission;
            }
        } catch (SQLException e) {
            log.error("execute selectOneById error: " + e.getMessage());
        }
        return null;
    }

    @Override
    public List<Permission> selectPermsByRoleName(String roleName) {
        String sql = "SELECT p.* FROM tb_permission p " +
                "JOIN tb_role_perm rp ON p.per_id = rp.perm_id " +
                "JOIN tb_role r ON r.role_id = rp.role_id " +
                "WHERE r.name = ?";
        List<Permission> permList = new ArrayList<>();
        try (PreparedStatement ps = this.conn.prepareStatement(sql)) {
            ps.setString(1, roleName);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Permission permission = new Permission();
                permission.setPerId(rs.getInt("per_id"));
                permission.setName(rs.getString("name"));
                permission.setUrl(rs.getString("url"));
                permList.add(permission);
            }
        } catch (SQLException e) {
            log.error("execute selectPermsByRoleName error: " + e.getMessage());
        }
        return permList;
    }

    @Override
    public List<Permission> selectPermsByRoleId(int roleId) {
        String sql = "SELECT p.* FROM tb_permission p " +
                "JOIN tb_role_perm rp ON p.per_id = rp.perm_id " +
                "WHERE rp.role_id = ?";
        List<Permission> permList = new ArrayList<>();
        try (PreparedStatement ps = this.conn.prepareStatement(sql)) {
            ps.setInt(1, roleId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Permission permission = new Permission();
                permission.setPerId(rs.getInt("per_id"));
                permission.setName(rs.getString("name"));
                permission.setUrl(rs.getString("url"));
                permList.add(permission);
            }
        } catch (SQLException e) {
            log.error("execute selectPermsByRoleId error: " + e.getMessage());
        }
        return permList;
    }

    @Override
    public Integer deleteById(int permId) {
        String sql = "DELETE FROM tb_permission WHERE per_id = ?";
        try (PreparedStatement ps = this.conn.prepareStatement(sql)) {
            ps.setInt(1, permId);
            return ps.executeUpdate();
        } catch (SQLException e) {
            log.error("execute deleteById error: " + e.getMessage());
        }
        return 0;
    }
}
